"use strict";
var xlsx = require("node-xlsx");
const { Service } = require("ee-core");

/**
 * 示例服务（service层为单例）
 * @class
 */
class ExampleService extends Service {
  constructor(ctx) {
    super(ctx);
  }

  async readExcel(path) {
    // read
    var obj = xlsx.parse(path, {
      // header: 0,
      header: [
        "name",
        "week1",
        "week2",
        "week3",
        "week4",
        "week5",
        "week6",
        "week7",
      ],
    });
    // console.log(JSON.stringify(obj));
    const arr = [];
    const _data = obj?.[0]?.data || [];
    console.log(_data);
    return _data;
    // const fields = _data.shift();
    // _data.forEach((x, xi) => {
    //   const item = {};
    //   fields.forEach((y, yi) => {
    //     item[y] = x[yi];
    //   });
    //   arr.push(item);
    // });

    // console.log(JSON.stringify(arr));
  }

  /**
   * test
   */
  async test(args) {
    let obj = {
      status: "ok",
      params: args,
    };
    const data = await this.readExcel(args.path);

    return { data };
  }
}

ExampleService.toString = () => "[class ExampleService]";
module.exports = ExampleService;
